﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Services.Interface;
using HanGu.Entity;
using HanGu.Common;
using System.Web.Caching;

namespace Hangu.WebClient.Controllers.Admin
{
    public class SystemConfigController : AdminBaseController
    {
        private static readonly ISystemConfig serviceSystemConfig = ServiceFactory.GetService<ISystemConfig>();

        //
        // GET: /SystemConfig/

        public ActionResult Index()
        {
            return View(viewList, GetSystemConfigList());
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {

            SearchParma.Pagination.RowCount = null;
            return View(viewList, GetSystemConfigList());
        }

        List<SystemConfig> GetSystemConfigList()
        {
            List<SystemConfig> systemConfigList = serviceSystemConfig.GetAll(base.SearchParma);
            ViewData["RowCount"] = SearchParma.Pagination.RowCount;
            return systemConfigList;
        }

        public ActionResult Create(string id)
        {
            SystemConfig systemConfig = new SystemConfig();
            if (string.IsNullOrEmpty(id) && id != "0")
            {
                ViewData["action"] = "edit";
                systemConfig = serviceSystemConfig.GetByID(id);
            }
            return View(systemConfig);
        }

        //
        // POST: /SystemConfig/Create

        [HttpPost]
        [ValidateInput(false)]
        public string Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                SystemConfig systemConfig = new SystemConfig();
                TryUpdateModel(systemConfig, collection);
                bool result = false;
                if (Request["action"] == "edit")
                {
                    result=serviceSystemConfig.Update(systemConfig);
                }
                else
                {
                    result=serviceSystemConfig.Add(systemConfig);
                }
                System.Web.HttpContext.Current.Cache.Remove("SystemConfigAll");
                if (result)
                {
                    ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", "closeCurrent");
                }
                else
                {
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
                }

            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";
        }



        //
        // GET: /SystemConfig/Delete/5
        [HttpPost]
        public string Delete(string id, string guid)
        {
            try
            {
                if (serviceSystemConfig.Delete(id))
                {
                    ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
                }
                else
                { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", ""); }
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }

            return "{" + ajaxStr + "}";

        }
    }
}
